package net.sochk.web.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sochk.web.dao.GetMeauDao;
import net.sochk.web.dao.ZipDecompressionToWebLocationDao;
import net.sochk.web.entity.Menu;
import net.sochk.web.entity.User;

public class MenuService {

	
  private GetMeauDao getMeauDao;
  private ZipDecompressionToWebLocationDao zipDecompressionToWebLocationDao;
  

  public GetMeauDao getGetMeauDao() {
	return getMeauDao;
}


public void setGetMeauDao(GetMeauDao getMeauDao) {
	this.getMeauDao = getMeauDao;
}



public ZipDecompressionToWebLocationDao getZipDecompressionToWebLocationDao() {
	return zipDecompressionToWebLocationDao;
}


public void setZipDecompressionToWebLocationDao(ZipDecompressionToWebLocationDao zipDecompressionToWebLocationDao) {
	this.zipDecompressionToWebLocationDao = zipDecompressionToWebLocationDao;
}


public List<Menu> getAllMenu(){
    return getMeauDao.getAllMenu();
  }//锟斤拷取锟斤拷锟叫菜碉拷锟斤拷
	
public String getAllMenuString(){
	   JSONArray object=new JSONArray();
	   
	   JSONObject allMenu = new JSONObject(); 
	   List<Menu> menus=getAllFirstMenu();
	   for(Menu menu:menus){
		   JSONObject node = new JSONObject(); 
		   menu.setMenus(null);
		   menu.setMenu(null);
		   node.put("menu", menu);
		   int menusecondid=menu.getMenuId();
		   List<Menu>haha=getAllSecondMenu(menusecondid); 
		   JSONArray objects=new JSONArray();
		   int i=0;
		   if(!haha.isEmpty()){
			 for(Menu me:haha){
				me.setMenus(null);
				 me.setMenu(null);
				 JSONObject nede = new JSONObject();
				 nede.put("a",me);
				 i++;
				 objects.add(nede);
			 }
			 node.put("child", objects);
		   }
		  
		  object.add(node); 
	   }
	   //object.add(menuService.getAllMenu());
	   System.out.println(object);
	  // return (JSONObject) allMenu.put("allmenu",object);
	   allMenu.put("allmenu",object);
	   return allMenu.toString();
  }//锟斤拷取锟斤拷锟叫菜碉拷锟斤拷
	  

public String getAllMenuString1(){
	   JSONArray object=new JSONArray();
	   
	   JSONObject allMenu = new JSONObject(); 
	   List<Menu> menus=getAllFirstMenu();
	   for(Menu menu:menus){
		   Menu menu1=new Menu();
		   JSONObject node = new JSONObject(); 
		   menu1.setMenu(null);
		   menu1.setMenus(null);
		  menu1.setMenuId(menu.getMenuId());
		  menu1.setMenuOrder(menu.getMenuOrder());
		  menu1.setMenuChain(menu.getMenuChain());
		  menu1.setMenuPhonechain(menu.getMenuPhonechain());
		  menu1.setMenuId(menu.getMenuId());
		  menu1.setMenuPicture(menu.getMenuPhonechain());
		 menu1.setMenuName(menu.getMenuName());
		   node.put("menu",menu1);
		   int menusecondid=menu.getMenuId();
		   List<Menu>haha=getAllSecondMenu(menusecondid); 
		   JSONArray objects=new JSONArray();
		   int i=0;
		   if(!haha.isEmpty()){
			 for(Menu me:haha){
				me.setMenus(null);
				 me.setMenu(null);
				 JSONObject nede = new JSONObject();
				 nede.put("a",me);
				 i++;
				 objects.add(nede);
			 }
			 node.put("child", objects);
		   }
		  
		  object.add(node); 
	   }
	   //object.add(menuService.getAllMenu());
	   System.out.println(object);
	  // return (JSONObject) allMenu.put("allmenu",object);
	   allMenu.put("allmenu",object);
	   return allMenu.toString();
}//锟斤拷取锟斤拷锟叫菜碉拷锟斤拷

  public List<Menu> getAllFirstMenu(){
	  return getMeauDao.getAllFirstMenu();
  }; //锟斤拷取锟斤拷锟叫碉拷一锟斤拷锟剿碉拷锟斤拷
	
	
	
	
  public List<Menu> getAllSecondMenu(int MenuId){
	  return getMeauDao.getAllSecondMenu(MenuId);
  }; //锟斤拷取锟斤拷锟叫讹拷锟斤拷锟剿碉拷锟斤拷
	
  
 /* public String savePhoto(MultipartFile file){
	 
		 System.out.println(getMeauDao.savePhotos(file));
	  
	  return getMeauDao.savePhotos(file);
  }*/
  
  public String AddOrUpdata(Menu menu,MultipartFile file,MultipartFile zip,MultipartFile zip1,HttpServletRequest request,String path){
	  String localphotoPath=null;
	  if(file!=null){
	   localphotoPath=getMeauDao.savePhotos(file,path);
	  }
	  if(menu.getMenuName().isEmpty()){
		  return "isempty";
	  }
	  
	
	 menu.setMenuPicture(localphotoPath);
	 if(zip.isEmpty()){
		  menu.setMenuPhonechain(null);  
	 }else{
	 String localPath=zipDecompressionToWebLocationDao.decompressionToWebLocation(zip,request);
	
	 if(localPath.equals("faultType")){
		 return "faultType";
	 }
	 if(localPath.equals("failDecompress")){
		 return "failDecompress";
	 }
	  menu.setMenuPhonechain(localPath);
	 }
	 
	 if(zip1==null){
		
		  menu.setMenuChain(null);  
	 }else{
		 System.out.println("fuzhipeng");
 String localPath1=zipDecompressionToWebLocationDao.decompressionToWebLocation(zip1,request);
 System.out.println("fuzhipeng"+localPath1);
	 if(localPath1.equals("faultType")){
		 return "faultType";
	 }
	 if(localPath1.equals("failDecompress")){
		 return "failDecompress";
	 }
	 menu.setMenuChain(localPath1);
	 }
	System.out.println(menu);
	
	
	
	if(menu.getMenu()==null){
		System.out.println("null");
		menu.setMenuOrder(getMeauDao.getAllFirstMenuCount()+1);
	}else{
		int count=getMeauDao.getAllSecondMenuCount(menu.getMenus().hashCode());
		menu.setMenuOrder(count+1);
	}
			   getMeauDao.AddOrUpdata(menu);
			   return "success";
		 } //锟皆菜碉拷锟斤拷锟斤拷锟斤拷薷锟�
	 
	 
	 public Menu getOne(Integer id){
		 return getMeauDao.getMeau(id);
	 }
	 
	 public String Updata(Menu menu,MultipartFile file,MultipartFile zip,MultipartFile zip1,HttpServletRequest request,String path){
		System.out.println(menu+"aa");
		 String localphotoPatha=menu.getMenuPicture();
		 String localPatha=menu.getMenuPhonechain();
		 String localPatha1=menu.getMenuChain();
		 System.out.println("1111"+localPatha1);
		 System.out.println("1111"+localPatha);
		 if(!file.isEmpty()){
		  String localphotoPath=getMeauDao.savePhotos(file,path);
		 if(localphotoPath.equals("faultphototType")){
			 return "faultphototType";
		 }
		
		 menu.setMenuPicture(localphotoPath);
		 }
		 if(file.isEmpty()){
			
			 menu.setMenuPicture(localphotoPatha);
		 }
		 if(!zip.isEmpty()){
		 String localPath=zipDecompressionToWebLocationDao.decompressionToWebLocation(zip,request);
		 
		 if(localPath.equals("faultType")){
			 return "faultType";
		 }
		 if(localPath.equals("failDecompress")){
			 return "failDecompress";
		 }
		 menu.setMenuPhonechain(localPath);
		 }
		/* if(zip.isEmpty()){
			 menu.setMenuPhonechain(localPatha);
		 }*/
		 
		 if(!zip1.isEmpty()){
			 System.out.println("fuzhipeng");
			 String localPath=zipDecompressionToWebLocationDao.decompressionToWebLocation(zip1,request);
			 
			 if(localPath.equals("faultType")){
				 return "faultType";
			 }
			 if(localPath.equals("failDecompress")){
				 return "failDecompress";
			 }
			 menu.setMenuPhonechain(localPath);
			 }
		/* if(zip1.isEmpty()){
				 menu.setMenuPhonechain(localPatha1);
			 }
		 */
		 System.out.println(menu);
		   getMeauDao.AddOrUpdata(menu);
		   return "success";
		 
		 }; //锟皆菜碉拷锟斤拷锟斤拷锟斤拷薷锟�
	 
	 public void DeleteMeau(int id){ 
		 getMeauDao.DeleteMeau(id);
	 }  //删锟斤拷锟剿碉拷
	 
	 public Menu getMeau(int id){
           return getMeauDao.getMeau(id);
	 }//锟斤拷取锟剿碉拷
	 
	 public Integer load(String uername,String password){
		 password=MD5Util.MD5(password);
		 return getMeauDao.load(uername, password);
	 }
	 
	 public String loading(String name,String password){
		 JSONObject object = new JSONObject();
			System.out.println(name+password);
				Integer a=load(name, password);
			if(a!=0){
				//JSONArray object=new JSONArray();
				 object.put("new1", "success");
				object.put("admin",getAdmin(a));
				return object.toString();
			}else{
				 object.put("new1","faith");
				return object.toString();
			}
	 }; //锟斤拷取锟斤拷锟叫癸拷锟斤拷员锟斤拷
	 
	 public List<User> getAllAdmin(){
		 return getMeauDao.getAllAdmin();
	 }; //锟斤拷取锟斤拷锟叫癸拷锟斤拷员锟斤拷
		public User getAdmin(int id){
			return getMeauDao.getAdmin(id);
		}; //锟斤拷取锟斤拷锟叫癸拷锟斤拷员锟斤拷

		public String AddOrUpdata(User user){
			if(getMeauDao.CheckUser(user.getAdminAccount())!=0){
				return "userexist";
			}
			else{
			user.setAdminPassword(MD5Util.MD5(user.getAdminPassword()));
			 getMeauDao.AddOrUpdata(user);
			 return "success";
			}
		}; //锟皆癸拷锟斤拷员锟斤拷锟斤拷锟斤拷薷锟�
		
		public String Updata(User user,String password,String adminuser){
			if(password!=null){
				user.setAdminPassword(MD5Util.MD5(password));
		       }
			if(adminuser!=null&&getMeauDao.CheckUser(adminuser)==0){
		user.setAdminAccount(adminuser);
		getMeauDao.AddOrUpdata(user);
		 return "success";}
			if(adminuser!=null&&getMeauDao.CheckUser(adminuser)!=0&&!adminuser.equals(user.getAdminAccount())){
			System.out.println("锟斤拷");
			return "usercountexist";
		}
			else{
			 getMeauDao.AddOrUpdata(user);
			 return "success";
			}
		
		}; //锟皆癸拷锟斤拷员锟斤拷锟睫革拷
		
		public void DeleteUser(int id){
			getMeauDao.DeleteUser(id);
		};//删锟斤拷锟斤拷锟斤拷员
	 
		
		public List<String> getproduct() {
			List<String>fu =new ArrayList<String>();
			List<String> haha=getMeauDao.getproduct();
					for(String hehe:haha){
						if(hehe != null && hehe.length() != 0){
						hehe=hehe.substring(hehe.lastIndexOf("/")+1);
					
						fu.add(hehe);}
					}
			return fu;
		}
		public List<String> getphoneproduct() {
			List<String>fu =new ArrayList<String>();
			List<String> haha=getMeauDao.getphoneproduct();
					for(String hehe:haha){
						if(hehe != null && hehe.length() != 0){
						hehe=hehe.substring(hehe.lastIndexOf("/")+1);
						System.out.println(hehe+"fuzhipeng");
						fu.add(hehe);
					}
					}
			return fu;
		}
		
		public String upOrder(int id){
		 return getMeauDao.UpOrder(id);
		}
		
public String downOrder(int id){
			return getMeauDao.downOrder(id);
		}

public String downallOrder(int id){
	 return getMeauDao.downallOrder(id);
	}

}

